import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:sports_new/utils/date_time_utils.dart';
import 'package:sports_new/utils/utils.dart';
import 'package:sports_new/utils/widget_utils.dart';
import 'package:sports_new/widgets/circular.dart';
import '../../../entity/match/expert_views_entity.dart';
import '../../../resources/colours.dart';
import '../../../utils/routes.dart';
import '../../../widgets/base/qkd_text.dart';

class LivePlanPopup extends StatelessWidget {
  const LivePlanPopup({super.key, required this.plan, required this.isHost, this.onTap});

  final PlanEntity? plan;
  final bool isHost;
  final ValueChanged<bool>? onTap;
  
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () => onTap?.call(true),
      child: Column(
        children: [
          Circular(
            gradient: const LinearGradient(
                colors: [Color(0xFFFF576F),Color(0xFFFFBFC8)]
            ),
            radius: 8,
            padding: const EdgeInsets.symmetric(vertical: 2),
            child: Row(
              children: [
                Image.asset(
                  Utils.getImgPath("live_plan_${isHost?"host":"other"}.png"),
                  width: 12
                ).paddingSymmetric(horizontal: 14),
                Expanded(
                  child: Stack(
                    alignment: Alignment.topRight,
                    children: [
                      Circular(
                        radius: 8,
                        color: Colours.white,
                        padding: const EdgeInsets.fromLTRB(12, 8, 20, 8),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            _contentWidget(),
                            Container(height: 10),
                            _matchInfoWidget(),
                            if(plan?.packInfo?.packageId != null)
                            packInfo()
                          ],
                        )
                      ),
                      GestureDetector(
                        behavior: HitTestBehavior.opaque,
                        onTap: () => onTap?.call(false),
                        child: Circular(
                          color: Colours.grey99,
                          margin: const EdgeInsets.only(left: 12,bottom: 12),
                          padding: const EdgeInsets.all(2),
                          borderRadius: const BorderRadius.only(
                            topRight: Radius.circular(7),
                            bottomLeft: Radius.circular(7)
                          ),
                          child: Image.asset(
                            width: 14,height: 14,
                            color: Colours.white,
                            Utils.getImgPath('bottomsheet_close.png')
                          )
                        ),
                      )
                    ],
                  ),
                ),
                Container(width: 2)
              ],
            ),
          ),
          // Container(
          //   alignment: Alignment.centerRight,
          //   padding: const EdgeInsets.only(right: 62),
          //   child: Image.asset(
          //     Utils.getImgPath("live_plan_popup.png"),
          //     width: 12,height: 4
          //   )
          // )
        ],
      ),
    );
  }

  Widget _contentWidget() {
    return RichText(
      maxLines: 2,
      softWrap: true,
      overflow: TextOverflow.ellipsis,
      text: TextSpan(
        children: [
          if (plan?.playTypeName != null)
            TextSpan(
              text: "[${plan?.playTypeName ?? ""}]",
              style: const TextStyle(fontSize: 14, color: Color(0xFF39407E))
            ),
          const TextSpan(text: "\u2000"),
          TextSpan(
            text: plan?.planTitle == '' || plan?.planTitle == null
              ? "暂无内容" : plan?.planTitle!,
            style: const TextStyle(fontSize: 14, color: Colours.text),
          ).formHighLight()
        ]
      )
    );
  }

  Widget _matchInfoWidget() {
    return Column(
      children: List.generate(
        plan?.matchBries?.length ?? 0,
          (index) => Container(
          width: Get.width,
          padding: const EdgeInsets.symmetric(vertical: 3,horizontal: 6),
          decoration: BoxDecoration(
            color: Colours.greyF7FAFF, borderRadius: BorderRadius.circular(5)
          ),
          child: _matchInfoItem(
            plan?.matchBries?[index],plan?.sportsId ?? 1
          )
        ),
      ).insertEvery(Container(height: 2))
    );
  }

  Widget packInfo(){
    return GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () => Get.toNamed(
            Routes.planPackDetail,
            arguments: plan?.packInfo?.packageId
        ),
        child: Container(
            margin: const EdgeInsets.only(top: 8),
            padding: const EdgeInsets.symmetric(
                horizontal: 12,vertical: 5
            ),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(13),
                border: Border.all(color: Colours.main,width: 0.5)
            ),
            child: Row(
                children: [
                  QkdText(
                    plan?.packInfo?.content??"",
                    fontSize: 12,color: Colours.main,
                    needCenter: true,
                  ),
                  const Spacer(),
                  Image.asset(
                    Utils.getImgPath("arrow_right_36.png"),
                    width: 12,height: 12,
                    color: Colours.main,
                  )
                ]
            )
        ),
      );
  }

  Widget _matchInfoItem(MatchBries? match,int sportsId) {
    return DefaultTextStyle(
      style: const TextStyle(fontSize: 10,color: Colours.grey99),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          QkdText("[${sportsId == 1? "足" : "篮"}]\u2000",
            fontSize: 12,
            color: Colours.blue2766D6
          ),
          QkdText(
            "${DateTime.parse(match?.matchTime ?? '').formatedString("MM/dd HH:mm") ?? "时间暂无"}\u2000",
            height: 1.5,
            fontSize: 12,
            color: Colours.grey99,
          ),
          Container(
              width: 0.5,
              height: 12,
              alignment: Alignment.center,
              color: Colours.greyE0E1E5),
          QkdText(
              "\u2000${match?.leagueName ?? "未知联赛"}\u2000",
            fontSize: 12, color: Colours.grey99,
          ),
          Flexible(
            child: QkdText(
              sportsId == 1? match?.homeName?? "主队未知" : match?.guestName?? "客队未知",
              maxLines: 1,
              fontSize: 12,
              color: Colours.grey99,
            ).formHighLight(),
          ),
          const QkdText("\u2000"),
          match?.status == 1 || plan?.planStatus == 3
              ? const QkdText("vs",fontSize: 12, color: Colours.grey99,)
              : sportsId == 1?QkdText(
            "${match?.homeScore90}:${match?.guestScore90}",
            fontSize: 12, color: Colours.grey99,
          ):QkdText(
            "${match?.guestScore}:${match?.homeScore}",
            fontSize: 12, color: Colours.grey99,
          ),
          const QkdText("\u2000"),
          Flexible(
            child: QkdText(
              sportsId == 1? match?.guestName?? "客队未知" : match?.homeName?? "主队未知",
              maxLines: 1,
              fontSize: 12,
              color: Colours.grey99,
            ).formHighLight(),
          ),
        ],
      ),
    );
  }
}
